package com.hkyp.ss.course.web.controller;

import com.hkyp.ss.course.apis.CourseService;
import com.hkyp.ss.course.apis.dto.common.PageReq;
import com.hkyp.ss.course.apis.dto.common.PageResp;
import com.hkyp.ss.course.apis.dto.request.HomePageCourseDTO;
import com.hkyp.ss.course.apis.dto.response.CourCommVoterVO;
import com.hkyp.ss.course.apis.dto.response.CourseSummaryVO;
import com.hkyp.ss.course.apis.dto.response.CourseVO;
import com.hkyp.ss.course.apis.utils.BeanMapper;
import com.hkyp.ss.course.web.dto.base.ResultBean;
import com.hkyp.ss.course.web.dto.request.WebHomePageCourseDTO;
import com.hkyp.ss.course.web.dto.response.WebCourseSummaryVO;
import com.hkyp.ss.course.web.utils.UserSessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Api(description = "课程")
@Slf4j
@RestController
@RequestMapping(value = "/a/h/")
public class CourseController {



    @Reference(version = "1.0")
    private CourseService courseService;

    /**
     * 课程点赞
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课程点赞",notes = "action=1表示点赞，action=0表示取消点赞")
    @RequestMapping(value = "/course/{cid}/api/study", method = RequestMethod.POST)
    public ResultBean<CourCommVoterVO> studyCourse(@PathVariable Long cid){

        Long userId = UserSessionUtils.getUserId();
        courseService.studyCourse(userId,cid);
        return new ResultBean<>();

    }
    /**
     * 课程列表-推荐
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课程列表-推荐", response = WebCourseSummaryVO.class)
    @RequestMapping(value = "/course/list/recommend", method = RequestMethod.GET)
    public ResultBean<List<CourseSummaryVO>> getRecommendCourses(){
        return new ResultBean<>(courseService.selectRecommendedCourse(UserSessionUtils.getUserIdCanBeNull()));

    }

    /**
     * 课程列表-多条件输入
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课程列表-多条件输入", notes = "按照需要输入条件，返回想要的结果。" +
            "补充：1.sortfield=comScore,按评分排序；2..sortfield=commentNum,按课评数排序")
    @RequestMapping(value = "/course/list", method = RequestMethod.GET)
    public ResultBean<PageResp<CourseSummaryVO>> getHomepageCourses(WebHomePageCourseDTO homePageCourseDTO, PageReq pageReq){

        return new ResultBean<>(courseService.selectHomePageCourseV2(BeanMapper.map(homePageCourseDTO, HomePageCourseDTO.class),pageReq,UserSessionUtils.getUserIdCanBeNull()));

    }

    /** TODO
     * 课程列表-按照标题或来源平台查询
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课程列表-按照标题或来源平台查询")
    @RequestMapping(value = "/course/list/keyword", method = RequestMethod.GET)
    public ResultBean<PageResp<CourseSummaryVO>> getHomepageCourses(PageReq pageReq){
        pageReq.setSortfield(null);
        pageReq.setSort(null);
        return new ResultBean<>(courseService.selectCourseByKeyword(pageReq.getKeyword(),pageReq));

    }


    /**
     * 课程详情
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课程详情")
    @RequestMapping(value = "/course/{id}", method = RequestMethod.GET)
    public ResultBean<CourseVO> getCourse(@PathVariable Long id){
        Long userId = UserSessionUtils.getUserIdCanBeNull();

        return new ResultBean<>(courseService.selectCourse(id,userId));

    }

     /**
     * 课程点赞
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "课程点赞",notes = "action=1表示点赞，action=0表示取消点赞")
    @RequestMapping(value = "/course/{cid}/api/like", method = RequestMethod.POST)
    public ResultBean<CourCommVoterVO> likeCourse(@PathVariable Long cid, Integer action){

        Long userId = UserSessionUtils.getUserId();
        return new ResultBean<>(courseService.likeCourse(cid,action,userId));

    }

    /**
     * 收藏课程
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "收藏课程",notes = "action=1表示收藏，action=0表示取消收藏")
    @RequestMapping(value = "/course/{cid}/api/collection", method = RequestMethod.POST)
    public ResultBean<Map> collectCourse(@PathVariable Long cid, Integer action){

        Long userId = UserSessionUtils.getUserId();
        return new ResultBean<>(courseService.collectCourse(cid,action,userId));

    }

}
